<?php

require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/parameters_exception.php");

require_once("lib/model/action.php");
require_once("model/article/dao/article_dao_factory.php");
require_once("model/articlefacade/to/article_chunk_to.php");

class Find_articles_by_keywords_action implements Action {

	private $where;
	private $type;
	private $keywords;
	private $start_index;
	private $count;
	
	public function __construct($where, $type, $keywords, $start_index, $count) {
		$this->where = $where;
		$this->type = $type;
		$this->keywords = $keywords;
		$this->start_index = $start_index;
		$this->count = $count;
	}
	
	public function do_execute() {
		try {
			$dao = Article_dao_factory::singleton()->get_dao();
			if (count($this->keywords) == 0) 
				throw new Parameters_exception('Not keywords to search');
			
			$description = false;
			$category = false;	
			foreach($this->where as $where) {
				if ($where == 'description')
					$description = true;
				if ($where == 'category')
					$category = true;
			}	
			
			//solo titulo
			if (!($description || $category)) {
				if ($this->type == 'and') 
					$article_list = $dao->find_by_keywords_title_and($this->keywords, $this->start_index, $this->count);
				else 
					$article_list = $dao->find_by_keywords_title_or($this->keywords, $this->start_index, $this->count);
			}
			else {
				//titulo y descripcion
				if ($description && !$category) {
					if ($this->type == 'and') 						
						$article_list = $dao->find_by_keywords_title_description_and($this->keywords, $this->start_index, $this->count);
					else
						$article_list = $dao->find_by_keywords_title_description_or($this->keywords, $this->start_index, $this->count);
				}
				else {
					//titulo y categoria
					if ($category && !$description) {
						if ($this->type == 'and')
							$article_list = $dao->find_by_keywords_title_category_and($this->keywords, $this->start_index, $this->count);
						else
							$article_list = $dao->find_by_keywords_title_category_or($this->keywords, $this->start_index, $this->count);
					}
					else {
						if ($this->type == 'and')
							$article_list = $dao->find_by_keywords_title_description_category_and($this->keywords, $this->start_index, $this->count);
						else
							$article_list = $dao->find_by_keywords_title_description_category_or($this->keywords, $this->start_index, $this->count);
					}
				}
			}
							
			$more = false;
			if (count($article_list) > ($this->count+1)) {
				$e = new Exception("Result list too big");
				throw new Internal_error_exception($e);
			}
			else {
				if(count($article_list) == ($this->count+1)) { 
					unset($article_list[$this->count]);
					$more = true;		
				}		
			}
			
			return new Article_chunk_to($article_list, $more);
		}
		catch (Parameters_exception $e) {
			throw $e;
		}
		catch (Instance_not_found_exception $e) {
			throw new Model_exception($e);
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
}

?>
